package com.product.system.aop;
import com.product.system.domain.response.SysRole;
import com.product.system.domain.response.SysRoleLog;
import com.product.system.service.SysRoleLogService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

@Aspect
@Component
public class RoleAddLogAspect {



    @Autowired
    SysRoleLogService sysRoleLogService;


    @Around("@annotation(com.product.system.aop.RoleAddLogAop)")
    public Object roleAddLog(ProceedingJoinPoint joinPoint) throws Throwable {

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();

        Method method = signature.getMethod();

        SysRole sysRole = null;
        Annotation[][] parameterAnnotations = method.getParameterAnnotations();
        if (parameterAnnotations.length > 0) {
            Object[] args = joinPoint.getArgs();
            for (Object arg : args) {
                if (arg instanceof SysRole) {
                     sysRole = (SysRole) arg;
                }
            }
            SysRoleLog sysRoleLog = SysRoleLog.builder()
                    .roleKey(sysRole.getRoleKey()).roleName(sysRole.getRoleName())
                    .createTime(sysRole.getCreateTime()).roleSort(sysRole.getRoleSort())
                    .remark(sysRole.getRemark())
                    .build();
            sysRoleLogService.addRoleLog(sysRoleLog);
        }

        Object proceed = joinPoint.proceed();

        return proceed;

    }

}
